﻿/***************************************************************************
 *		ElGee Google Chart API Helper
 *		Copyright(C) 2011 by Laurent Geffroy lgeffroy@gmail.com
 * 
 *		This program is free software; you can redistribute it and/or 
 *		modify it under the terms of the GNU General Public License as
 *		published by the Free Software Foundation; either version 2 of 
 *		the License, or (at your option) any later version.
 *		
 *		This program is distributed WITHOUT ANY WARRANTY; without even the
 *		implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *		See the GNU General Public License for more details.
 *	
 *      For particular licensing, please contact me at lgeffroy@gmail.com
 *****************************************************************************/
using System;
using System.Text;

namespace ElGee.Google.Charts
{
    /// <summary>
    /// The Background of the Chart
    /// </summary>
    public class ChartBackground : ChartBase
    {

        private readonly string[] _colorExclusion = { "white", "#ffffff" };
        private readonly string[] _strokeExclusion = { "#666", "#666666" };

        /// <summary>
        /// Gets or sets the color.
        /// The background color for the main area of the chart. Can be either a simple HTML color string, for example: 'red' or '#00cc00', or an object with the following properties.
        /// </summary>
        /// <value>backgroundColor</value>
        public String Color { get; set; }


        /// <summary>
        /// Gets or sets the stroke.
        /// The color of the chart border, as an HTML color string.
        /// </summary>
        /// <value>backgroundColor.stroke</value>
        public String Stroke { get; set; }

        /// <summary>
        /// Gets or sets the width of the stroke.
        /// The border width, in pixels.
        /// </summary>
        /// <value>backgroundColor.strokeWidth</value>
        public Int16 StrokeWidth { get; set; }

        /// <summary>
        /// Gets a value indicating whether this instance is empty.
        /// </summary>
        /// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
        public bool IsEmpty
        {
            get
            {
                if ((!String.IsNullOrWhiteSpace(Color)) && (!String.IsNullOrWhiteSpace(Stroke)) && (StrokeWidth > 0) && (Array.IndexOf(_colorExclusion, Color.ToLower()) == -1) && (Array.IndexOf(_strokeExclusion, Stroke.ToLower()) == -1))
                    return false;
                return true;
            }

        }
        public override string ToString()
        {
            if (IsEmpty)
                return String.Empty;

            StringBuilder sb = new StringBuilder();
            sb.Append("backgroundColor: {");
            if ((!String.IsNullOrWhiteSpace(Color)) && (Array.IndexOf(_colorExclusion, Color.ToLower()) == -1))
                sb.Append("fill : '").Append(SecureString(Color)).Append("',");

            if ((!String.IsNullOrWhiteSpace(Stroke)) && (Array.IndexOf(_strokeExclusion, Stroke.ToLower()) == -1))
                sb.Append("stroke : '").Append(SecureString(Stroke)).Append("',");

            if (StrokeWidth > 0)
                sb.Append("strokeWidth : ").Append(StrokeWidth).Append(",");

            // Removing the last comma
            sb.Remove(sb.Length - 1, 1);
            sb.Append("}");

            return sb.ToString();
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="ChartBackground"/> class.
        /// </summary>
        public ChartBackground()
        {
            StrokeWidth = 0;
        }
    }

}
